// pages/selectSeat/selectSeat.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    dateInfo: '',
    movieArrangeInfo: [],
    seatList: [],
    scale: 1,
    selectSeatList: [],
    selectOrNot: 0,
    sumPrice: '',
    ct: '12%',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    var that = this
    that.setData({
      movieArrangeInfo: wx.getStorageSync('selectMovieArrangeInfo'),
      dateInfo: options.dateInfo
    })
    wx.showLoading({
      title: '加载中',
    })
    wx.request({
      url: 'http://localhost:8081/api/seat/wxGetSeatList',
      data: {
        movieArrangeId: that.data.movieArrangeInfo.id
      },
      method: 'GET',
      success: function (res) {
        that.setData({
          seatList: res.data.result
        })
        wx.hideLoading({
          success: (res) => {},
        })
      }
    })
    console.log(that.data.movieArrangeInfo)
  },

  // 提交并跳转
  toCheckOrder() {
    var that = this
    if (that.data.selectOrNot != '') {
      wx.setStorageSync('selectSeatList', that.data.selectSeatList)
      wx.redirectTo({
        url: '../checkOrder/checkOrder?sumPrice=' + that.data.sumPrice + '&dateInfo=' + that.data.dateInfo,
      })
    }
  },

  // 选择座位
  selectSeat(e) {
    var that = this
    if (e.currentTarget.dataset.selectseatinfo.status == 1) {
      if (that.data.selectSeatList.length > 5) {
        wx.showToast({
          title: '最多选6张票噢',
          icon: 'error',
        })
        return
      }
      that.data.seatList[e.currentTarget.dataset.selectseatinfo.seatX - 1][e.currentTarget.dataset.selectseatinfo.seatY - 1].status = 3;
      that.data.selectSeatList.push(e.currentTarget.dataset.selectseatinfo)
      that.setData({
        seatList: that.data.seatList,
        scale: 1.3,
        selectSeatList: that.data.selectSeatList,
        sumPrice: that.data.movieArrangeInfo.price * that.data.selectSeatList.length
      })
      if (that.data.selectSeatList.length > 0) {
        that.setData({
          selectOrNot: 1,
          ct: '3%'
        })
      }
    }
    if (e.currentTarget.dataset.selectseatinfo.status == 3) {
      that.data.seatList[e.currentTarget.dataset.selectseatinfo.seatX - 1][e.currentTarget.dataset.selectseatinfo.seatY - 1].status = 1
      for(var i = 0;i < that.data.selectSeatList.length; i++){
        if (that.data.selectSeatList[i].id == e.currentTarget.dataset.selectseatinfo.id) {
          that.data.selectSeatList.splice(i,1)
          break
        }
      }
      that.setData({
        seatList: that.data.seatList,
        selectSeatList: that.data.selectSeatList,
        sumPrice: that.data.movieArrangeInfo.price * that.data.selectSeatList.length
      })
      if (that.data.selectSeatList.length == 0) {
        that.setData({
          selectOrNot: 0
        })
      }
    }
    that.setData({
      scale: 1.3,
    })
  },


  // 移除已选
  removeSelectSeat(e) {
    var that = this
    that.data.selectSeatList.splice(e.currentTarget.dataset.remo[0].index,1)
    that.data.seatList[e.currentTarget.dataset.remo[1].item.seatX - 1][e.currentTarget.dataset.remo[1].item.seatY - 1].status = 1
    that.setData({
      selectSeatList: that.data.selectSeatList,
      seatList: that.data.seatList,
    })
    if (that.data.selectSeatList.length == 0) {
      that.setData({
        selectOrNot: 0,
        sumPrice: ''
      })
    } else {
      that.setData({
        sumPrice: that.data.selectSeatList.length * that.data.movieArrangeInfo.price
      })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})